<?php 
	
class AdminAdd
{

	var $pdo = null;
    var $logger = null;
    function __construct()
    {
        $this->pdo = PDOWrapper::instance();
        $this->logger = CLog::getInstance();
    }

    /**
     * @param $GetParams
     * @param $PostArray
     * @return bool
     * 添加代理
     */
    function AddAgent($PostArray) {

        $agent = $this->CheckAgent($PostArray);
        if(false === $agent) {
            return false;
        } 
        $user_id = User::GetUidByMobile($PostArray['mobile']);
        if(empty($user_id)){
            Utils::ShowError(ErrorDefine::USER_NOT_EXIST);
            return false;
        }
        $params = array('id');
        $where = array(
            'user_id' => $user_id[0]['user_id']
        );
        $agent = $this->pdo->select('agent',$params,$where);
        if(!empty($agent)){
            Utils::ShowError(ErrorDefine::MOBILE_EXISTS);
            return false;
        }

        $params = array(
            'user_id' => $user_id[0]['user_id'],
            'city' => $PostArray['city'],
            'province' => $PostArray['province'],
            'remarks' => $PostArray['remark']
        );
        $agent_descri = $this->pdo->insert('agent',$params);

        return $agent_descri;
    }

    /**
     * @param $GetParams
     * @param $PostArray
     * @return bool
     * 后台管理员添加
     */
    function AddAdmin($PostArray)
    {
        if(empty($PostArray['mobile'])){
            Utils::ShowError(ErrorDefine::REQUIRED_FIELD_EMPTY);
            return false;
        }
        $mobile = $PostArray['mobile'];
        $error_code = Common::CheckMobile($mobile);

        if(empty($PostArray['admin_type'])){
            Utils::ShowError(ErrorDefine::REQUIRED_FIELD_EMPTY);
            return false;
        }
        $admin_type = $PostArray['admin_type'];
        $params = ['dept_name'];
        $where = [
            'dept_id' => $admin_type
        ];
        $check_admin_type = $this->pdo->select('department',$params,$where);
        if(empty($check_admin_type)){
            Utils::ShowError('455','非法部门');
            return false;
        }

        $params = array('admin_id');
        $where = array(
            'mobile' => $mobile
        );
        $res = $this->pdo->select('admins',$params,$where);
        if(!empty($res)){
            Utils::ShowError(ErrorDefine::MOBILE_EXISTS);
            return false;
        }

        $params = array(
            'user_id',
            'name',
            'email',
            'password',
            'portrait'
        );
        $res = $this->pdo->select('users',$params,$where);
        if(empty($res)){
            Utils::ShowError(ErrorDefine::USER_NOT_EXIST);
            return false;
        }
        $params = array(
            'admin_id' => $res[0]['user_id'],
            'name' => $res[0]['name'],
            'email' => $res[0]['email'],
            'password' => $res[0]['password'],
            'portrait' => $res[0]['portrait'],
            'mobile' => $mobile,
            'admin_type' => $admin_type
        );
        $admin_descri = $this->pdo->insert('admins',$params);

        return $admin_descri;
    }

    /**
     * @param $GetParams
     * @param $PostArray
     * @return bool
     * 添加新部门
     */
    function AddDepartment($PostArray){

        if(empty($PostArray['department'])){
            Utils::ShowError(ErrorDefine::REQUIRED_FIELD_EMPTY);
            return false;
        }
        $field = ['dept_id'];
        $order_by = ['dept_id' => 'DESC'];
        $dept_id = $this->pdo->select('department',$field,null,1,null,$order_by);
        $dept_id = $dept_id['dept_id'];
        $new_dept = $dept_id + 1000;
        $params = [
            'dept_id' => $new_dept,
            'dept_name' => $PostArray['department']
        ];
        $ret = $this->pdo->insert('department',$params);

        return $params;
    }


    private function CheckAgent($PostArray){

        if(!isset($PostArray['mobile'])){
            Utils::ShowError(ErrorDefine::REQUIRED_FIELD_EMPTY);
            return false;
        }
        if(!isset($PostArray['city'])){
            Utils::ShowError(ErrorDefine::REQUIRED_FIELD_EMPTY);
            return false;
        }
        if(!isset($PostArray['province'])){
            Utils::ShowError(ErrorDefine::REQUIRED_FIELD_EMPTY);
            return false;
        }
        if(!isset($PostArray['remark'])){
            Utils::ShowError(ErrorDefine::REQUIRED_FIELD_EMPTY);
            return false;
        }
    }

    

}